<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of users_model
 *
 * @author hassanali
 */
class user_model extends admin_model {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->table = 'user';
        $this->order = array('id' => metohd_get_uri('sort', 'desc'));
        $this->columns_op = array(
            'user_id' => 'Callback_get_userid_with_username',
            'create_date >=' => 'Callback_get_date',
            'code' => 'Callback_get_explode'
        );
    }
    
    function get_stat() {
        $pass = array();
        $pass['all_users'] = $this->get(FALSE, TRUE, 'result', FALSE, FALSE, false, false, TRUE);
        $pass['trash_users'] = $this->get(FALSE, TRUE, 'result', FALSE, FALSE, TRUE);
        $pass['user_users'] = $this->get(array('mode' => 'user'), TRUE);
        $pass['admin_users'] = $this->get(array('mode' => 'admin'), TRUE);
        return $pass;
    }

    protected function get_data_report($data = '', $action = '') {
        $subject = lang('report_' . $action);
        $data = json_decode($data);
        if (is_object($data)) {
            $data = get_object_vars($data);
        }
        if (is_array($data)) {
            $replace_key = array();
            $replace_val = array();
            foreach ($data as $key => $value) {
                if ($key == 'statue_id') {
                    $replace_key[] = '%' . $key;
                    
                    $statues = get_statues($value, '1');
                    $replace_val[] = $statues->title;
                } else {
                    $replace_key[] = '%' . $key;
                    $replace_val[] = $value;
                }
            }
           
            return str_replace($replace_key, $replace_val, $subject);
        } else {
            return str_replace('%s', $data, $subject);
        }
    }

    public function manager_report($data) {
        $result = array();

        if (count($data) > 1) {
            foreach ($data as $key => $value) {
                $result[$key]['id'] = $value->id;
                $result[$key]['create_date'] = $value->create_date;
                $result[$key]['report'] = $this->get_report($value->action);
                $result[$key]['data'] = $this->get_data_report($value->contant, $value->action);
            }
        } else {
            $result[0]['id'] = $data->id;
            $result[0]['create_date'] = $data->create_date;
            $result[0]['report'] =lang('action_'.$data->action);
            $result[0]['data'] =$this->get_data_report($data->contant, $data->action); //json_decode($data->contant);
        }


        return $result;
    }
    
    protected function get_report($action) {
        return lang('action_' . $action);
    }

    /**
     * 
     * @param type $user_id
     * @return type
     */
    public function order_statues($user_id) {


        $this->db->select('statues.title as statues_title , COUNT(items.id)  as S_count , items.statue_id', false);
        $this->db->join('statues', '  statues.id = items.statue_id');
        $this->db->group_by('statue_id');
        $sql = $this->db->get_where('items', array('items.user_id' => $user_id));
        return $sql->result();
    }
    
    function beforedelete($param = array()) {
        parent::beforedelete($param);
    }

    

}

?>
